<?php
class NewsLetterController extends DomController {
	
	public function index() {
		$this->emails();
	}
	
	public function emails() {
		
		
		if( $_GET['delete'] && $item = NewsLetter::load( $_GET['delete'] )){
			$item->delete();
		}
						
		$source = new DomArDataSource( NewsLetter::query()->orderBy('t.datetime DESC'), 40 );
		
		$panel = array(
			'title' => 'Uudiskiri',
			'layout' => 'table',
			'style' => 'width:700px;',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('edit/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('key' => 'title', 'title' => 'Pealkiri'),
						array('key' => 'datetime', 'title' => 'Loodud'),
						array('type' => 'gridButton', 'value' => 'Vaata', 'link' => $this->getUrl('view/'), 'title' => 'Valikud','colspan' => 3),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('edit/') ),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('emails/&delete='), 'confirmMessage' => 'Are you sure?' ),
					),
				),
			),
		);
					
		$this->view->contents = DomFg::render( $panel );
		
		
	}
	
	public function edit( $id = NULL ) {
		$new = $id == 0;
		$item = $new ? new NewsLetter() : NewsLetter::load($id);
				
		$post = DomFgPost::getInstance();
		
		if( $_GET['deleteElement'] && $element = DomAr::load($_GET['deleteElement'], 'NewsLetterItem') ) {
			$element->delete();
			$message = array('type' => 'warning','colspan' => 5,'message' => 'Sektsioon kustutatud',);
		}
		
		if( $_GET['moveUp'] && $element = DomAr::load($_GET['moveUp'], 'NewsLetterItem') ) {
			$element->order->decrease();
			$message = array('type' => 'ok','colspan' => 5,'message' => 'Sektsioon liigutatud ülespoole',);
		}
		
		if( $_GET['moveDown'] && $element = DomAr::load($_GET['moveDown'], 'NewsLetterItem') ) {
			$element->order->increase();
			$message = array('type' => 'ok','colspan' => 5,'message' => 'Sektsioon liigutatud allapoole',);
		}
		
		if( $post->addNew ) {
			$element = new NewsLetterItem_Basic();
			switch( $_POST['templateSelection'] ) {
				case 'left': $element->imageLocation = 'left'; break;
				case 'right': $element->imageLocation = 'right'; break;
				case 'top': $element->imageLocation = 'top'; break;
				case 'bottom': $element->imageLocation = 'bottom'; break;
			}
			$item->items[] = $element;
			$item->save();
		}
		
		
		if( $_POST ) {
			
			$item->title = $post->title;
			$item->template = $post->template;
			
			foreach( $item->items as $element ) {
				$element->title = $post->{'title_'.$element->id};
				$element->contents = $post->{'contents_'.$element->id};
				$element->link = $post->{'link_'.$element->id};
				$element->image->addUploaded( $post->{'image_'.$element->id}[0], $post->{'image_'.$element->id}[1]);
			}
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 5,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 5,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
		
		
		$panel = array(
			'title' => 'Uudiskiri',
			'layout' => 'table',
			'action' => $this->getUrl('edit/'.$id),
			'layoutConfig' => array(
				'columns' => 5,
				'columnWidths' => array( 150,220,150,220 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'button',
					'title' => 'Tagasi',
					'name' => 'back',
					'colspan' => 4,
					'link' => $this->getUrl('emails'),
				),
				array(
					'type' => 'button',
					'title' => 'Salvesta',
					'name' => 'save2'
				),
				array(
					'type' => 'textField',
					'title' => 'Pealkiri',
					'value' => $item->title,
					'name' => 'title',
				),
				array(
					'type' => 'select',
					'title' => 'Kujundus',
					'values' => $item->getTemplates(),
					'selected' => $item->template,		
					'name' => 'template',
					'colspan' => 2,
				),
				$panel['items'][] = array(
					'type' => 'separator',	
					'title' => 'Sektsioonid',
					'colspan' => 5,
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
				array(
					'title' => 'Tagasi',
					'name' => 'back',
					'style' => 'float:left',
					'link' => $this->getUrl('emails'),
				),
			),
		);

		// ---------------------------------------------
		// ELEMENTS
		// ---------------------------------------------
		
		$elements = $item->items->query()->orderBy('t.order')->commit()->toArray();
		if( count($elements) > 0 ) {
			foreach( $elements as $element ) {
				$panel['items'][] = $this->getItemPanel_Basic( $element );
			}
		} else {
			$panel['items'][] = array(
				'type' => 'info',
				'message' => 'Veel ei ole lisatud ühtegi sektsiooni!',
				'description' => 'Sektsiooni lisamiseks valige alt kujundus ja vajutage nuppu "Lisa uus"',	
				'colspan' => 5,
			);
		}
		
		// ---------------------------------------------
		// ADD NEW PANEL
		// ---------------------------------------------
		
		$templateSelection = new DomView( '/' . NewsLetter::TEMPLATES_DIR . 'templateSelection.html'  );
		$templateSelection->imagesUrl = $this->getMediaUrl( NewsLetter::TEMPLATES_DIR );
		$templateSelection = $templateSelection->fetch();
		$panel['items'][] = array(
			'type' => 'separator',	
			'title' => 'Uus sektsioon',
			'colspan' => 5,
		);
		$panel['items'][] = array(
			'type' => 'value',
			'value' => $templateSelection,
			'colspan' => 4,
		);
		$panel['items'][] = array(
			'textBefore' => '<br><br>',
			'type' => 'button',	
			'name' => 'addNew',
			'title' => 'Lisa uus',
		);
		$panel['items'][] = array(
			'type' => 'separator',	
			'colspan' => 5,
			'style' => 'height:45px;',
		);
		// ---------------------------------------------
		// ---------------------------------------------
		
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	private function getItemSourcePanel() {
		$panel = array(
			'type' => 'fieldSet',
			'title' => 'Ala',
			'style' => 'margin: 10px 0 50px 0;',
			'frame' => false,
			'layout' => 'table',
			'action' => $this->getUrl('getDomFgForm/'.$id),
			'layoutConfig' => array(
				'columns' => 5,
				'columnWidths' => array( 150 ),
			),
			'colspan' => 5,
			'items' => array(),
		);
		return $panel;
	}
	
	private function getItemPanel_Basic( NewsLetterItem_Basic $item ) {
		$panel = $this->getItemSourcePanel();
		
		$image = array(
			'type' => 'image',
			'src' => $item->image->getUrl(),
		);
		$buttons  = array(
			'type' => 'panel',
			'frame' => false,
			'border' => false,
			'colspan' => 5,
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Salvesta',
					'style' => 'float:right',
				),
				array(
					'type' => 'button',
					'title' => 'Kustuta',
					'style' => 'float:right',
					'link' => $this->getUrl('edit/'.$item->letter->id.'&deleteElement='.$item->id),
					'confirmMessage' => 'Olete kindel, et soovite kustutada?',
				),
				array(
					'type' => 'button',
					'title' => 'Alla',
					'style' => 'float:right',
					'link' => $this->getUrl('edit/'.$item->letter->id.'&moveDown='.$item->id),
				),
				array(
					'type' => 'button',
					'title' => 'Üles',
					'style' => 'float:right',
					'link' => $this->getUrl('edit/'.$item->letter->id.'&moveUp='.$item->id),
				),
			),
		);
		
		$file = array(
			'type' => 'file',
			'title' => 'Pildifail',
			'name' => 'image_'.$item->id,
		);
		$link = array(
			'type' => 'textField',
			'title' => 'Link',
			'value' => $item->link,
			'name' => 'link_'.$item->id,
		);
		$title = array(
			'type' => 'textField',
			'title' => 'Pealkiri',
			'value' => $item->title,
			'name' => 'title_'.$item->id,
			'colspan' => 4,
		);
		$editor = array(
			'type' => 'htmlEditor',
			'value' => $item->contents,
			'name' => 'contents_'.$item->id,
		);
		
		if( $item->imageLocation == 'left' or $item->imageLocation == 'right' ) {
			$image['style'] = 'width:200px; height:320px;';
			$image['rowspan'] = 3;
			$title['style'] = 'width:400px';
			$editor['colspan'] = $item->imageLocation == 'right' ? 4 : 5;
			
			
		} else {
			$image['style'] = 'width:800px; height:150px;';
			$image['colspan'] = 5;
			$editor['colspan'] = 5;
			$title['style'] = 'width:600px';
			$link['colspan'] = 2;
			
			
		}
		
		switch( $item->imageLocation ) {
			case 'left'; 
				array_push( $panel['items'], $image, $file, $link, $title, $editor, $buttons );
			break;
			case 'right';
				array_push( $panel['items'], $file, $link, $image, $title, $editor, $buttons );
			break;
			case 'top'; 
				array_push( $panel['items'], $image, $file, $link, $title, $editor, $buttons );
			break;
			case 'bottom'; 
				array_push( $panel['items'], $title, $editor, $image, $file, $link, $buttons );	
			break;
		}
			
			
		return $panel;
	}
	
	
	public function view( $id = NULL ) {
		$letter = NewsLetter::load( $id );
		
		$this->view->setTemplate( false );
		
		echo $letter->render();
	}
	
}
?>